package com.hqully.smartfumehood.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqully.smartfumehood.common.utils.QueryVoUtil;
import com.hqully.smartfumehood.common.utils.TimeUtil;
import com.hqully.smartfumehood.common.vo.QueryVo;
import com.hqully.smartfumehood.entity.DevRecord;
import com.hqully.smartfumehood.entity.dto.DevRecordDto;
import com.hqully.smartfumehood.mapper.DevRecordMapper;
import com.hqully.smartfumehood.service.DevRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lly
 * @since 2022-03-09
 */
/**
 * <p>
 *     设备记录服务实现类
 * </p>
 * @author liulingyu
 * @date 2022-05-12 17:33
 * @version 1.0
 */
@Service
public class DevRecordServiceImpl extends ServiceImpl<DevRecordMapper, DevRecord> implements DevRecordService {

    @Autowired
    private DevRecordMapper devRecordMapper;


    @Override
    public PageInfo<DevRecordDto> findAllRecord(QueryVo queryVo) {

        PageHelper.startPage(queryVo.getPageNum(),
                queryVo.getPageSize(),true);

        List<DevRecordDto> devRecordList = devRecordMapper.findAllRecord(queryVo.getQuery());



        PageInfo<DevRecordDto> devRecordPageInfo = new PageInfo<>(devRecordList);


        return devRecordPageInfo;
    }

    @Override
    public PageInfo<DevRecord> findRecordByDid(QueryVo queryVo) {
        PageInfo<DevRecord> recordPageInfo = QueryVoUtil.getPages(
                queryVo.getPageNum(),
                queryVo.getPageSize(),
                devRecordMapper.findRecordByDid(((Map<String, String>)queryVo.getQuery()).get("devRecordId")));
        return recordPageInfo;

    }

    @Override
    public DevRecord findLatestRecordByDid(String did) {
        return devRecordMapper.findLatestRecordByDid(did);
    }

    @Override
    public int addRecord(DevRecord record) {

        return devRecordMapper.addRecord(record);
    }

    @Override
    public int deleteRecord(long recordId) {
        return devRecordMapper.deleteRecord(recordId);
    }

    @Override
    public int updateRecord(DevRecord record) {
        String duration = TimeUtil.formatDuration2Seconds(record.getStartTime(), record.getEndTime());
        record.setDuration(duration);
        return devRecordMapper.updateRecord(record);
    }
}
